<?php

class Admin_Model_Category extends Zend_Db_Table_Abstract
{
    protected $_name = "core_categories";
    
    /**
     * 创建分类
     *
     * @param string $type
     * @param string $name
     */
    public function createCategory($fid, $name)
    {
        $nextId = $this->getAdapter()->fetchOne('SELECT MAX(id) FROM '.$this->_name );
        $row = $this->createRow();
        $row->fid = $fid;
        $row->name = $name;
        $row->path = $fid.','.$nextId;
        $row->save();
        if($row){
            return TRUE;
        }
        else{
            return FALSE;
        }
    }
    
    /**
     * 更新分类
     *
     * @param int $id
     * @param int $fid
     * @param string $type
     * @param string $name
     */
    public function updateCategory($id, $fid, $name) {
        $row = $this->find($id)->current();
        if ($row) {
            $row->fid = $fid;
            $row->name = $name;
            $row->save();
            
            return $row->id;
        }
        else{
            return false;
        }
    }
    
    /**
     * 删除分类
     *
     * @param int $id
     */
    public function deleteCategory($id) {
        $row = $this->find($id)->current();
        if ($row) {
            $row->delete();
        }
        else{
            return null;
        }
    }
}

